package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IProductService;
import com.wondertek.poms.dao.po.Product;
import com.wondertek.poms.dao.pojo.ProductPojo;
import com.wondertek.poms.dao.productrepo.IProductDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl extends ServiceImpl<Product,Long> implements IProductService {

	@PersistenceContext(name = "entityManagerSecondary", unitName = "secondaryPersistenceUnit")
    public EntityManager entityManager;
    @Autowired
    private IProductDao productDao;

    @Override
    public Page<ProductPojo> findAll(ProductPojo productPojo, int page, int rows, String sord, String sidx) {
        StringBuilder condition = sqlStringWhere(productPojo,sord,sidx);
        Object o = entityManager
                .createNativeQuery("select count(*) from ( " + condition.toString() + " ) a").getSingleResult();
        int count = Convert.toInt(o);
        Pageable pageable = getPageable(page - 1, rows, sord, sidx);

        if(count>0){
            Query query=entityManager.createNativeQuery(condition.toString());
            query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
            query.setMaxResults(pageable.getPageSize());
            List<Object[]> resultList=query.getResultList();
            List<ProductPojo> list = getList(resultList);
            PageImpl<ProductPojo> result=new PageImpl<>(list,pageable,count);
            entityManager.close();
            return result;
         }
        entityManager.close();
        return Page.empty(pageable);
    }

    @Override
    public List<Long> getAllProductId(String vo) {
        List<Long> list;
        if(StrUtil.isNotBlank(vo)){
            vo="%"+vo+"%";
            list=productDao.getAllProductLike(vo);
        }else {
            list=productDao.getAllProduct();
        }
        return list;
    }

    @Override
    public List<ProductPojo> getByProductIds(String productIds) {
        String[] idArray = productIds.split(",|，");
        List<ProductPojo> list=new ArrayList<>();
        for(String id:idArray){
            Product temp=productDao.findByProductID(id);
            ProductPojo pojo=new ProductPojo();
            if(temp != null){
                pojo.setProductID(temp.getProductID());
                pojo.setName(temp.getName());
                pojo.setPrice(temp.getPrice());
                pojo.setDescription(temp.getDescription());
                list.add(pojo);
            }
        }
        return list;
    }

    @Override
    public Product getByProductId(String productId) {
        return productDao.findByProductID(productId);
    }

    @Override
    public List<Product> findAllByProductId(String productId) {
        return productDao.findAllByProductId(productId);
    }


    public List<ProductPojo> getList(List<Object[]> resultList){
        List<ProductPojo> list=new ArrayList<>();
        for(Object[] o:resultList){
            ProductPojo productPojo=new ProductPojo();
            productPojo.setName(Convert.toStr(o[0]));
            productPojo.setProductID(Convert.toStr(o[1]));
            productPojo.setChargeMode(Convert.toStr(o[2]));
            productPojo.setPrice(Convert.toStr(o[3]));
            productPojo.setDescription(Convert.toStr(o[4]));
            list.add(productPojo);
        }
        return list;
    }

    public StringBuilder sqlStringWhere(ProductPojo productPojo,String sord, String sidx){

        StringBuilder condition=new StringBuilder();
        String sql="select NAME,PRODUCT_ID,CHARGE_MODE,PRICE,DESCRIPTION from PRODUCT where 1=1 ";
        condition.append(sql);

        if(StrUtil.isNotBlank(productPojo.getChargeMode())){
            condition.append("and CHARGE_MODE="+productPojo.getChargeMode()+" ");
        }
        if(StrUtil.isNotBlank(productPojo.getProductID())){
            condition.append("and PRODUCT_ID="+productPojo.getProductID()+" ");
        }
        condition.append("order by "+sidx+" "+sord);
        return condition;
    }

    @Override
    public Product deleteLogic(Long id) {
        return null;
    }

    @Override
    public Product merge(Product model) {
        return null;
    }
}
